package com.ruoyi.project.cardbox.request.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.cardbox.inbound.service.IOrderInboundService;
import com.ruoyi.project.cardbox.orders.domain.OrderStatus;
import com.ruoyi.project.cardbox.orders.domain.Orders;
import com.ruoyi.project.cardbox.orders.service.IOrdersService;
import com.ruoyi.project.cardbox.outbound.service.IOrderOutboundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.cardbox.request.mapper.OrderRequestMapper;
import com.ruoyi.project.cardbox.request.domain.OrderRequest;
import com.ruoyi.project.cardbox.request.service.IOrderRequestService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 请购单Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-04
 */
@Service
public class OrderRequestServiceImpl extends ServiceImpl<OrderRequestMapper, OrderRequest>  implements IOrderRequestService
{
    @Autowired
    private OrderRequestMapper orderRequestMapper;
    @Autowired
    private IOrdersService ordersService;
    @Autowired
    private IOrderInboundService orderInboundService;
    @Autowired
    private IOrderOutboundService orderOutboundService;

    /**
     * 查询请购单
     * 
     * @param id 请购单主键
     * @return 请购单
     */
    @Override
    public OrderRequest selectOrderRequestById(Long id)
    {
        return orderRequestMapper.selectOrderRequestById(id);
    }

    /**
     * 查询请购单列表
     * 
     * @param orderRequest 请购单
     * @return 请购单
     */
    @Override
    public List<OrderRequest> selectOrderRequestList(OrderRequest orderRequest)
    {
        return orderRequestMapper.selectOrderRequestList(orderRequest);
    }

    /**
     * 新增请购单
     * 
     * @param orderRequest 请购单
     * @return 结果
     */
    @Override
    public int insertOrderRequest(OrderRequest orderRequest)
    {
        orderRequest.setCreateTime(DateUtils.getNowDate());
        return orderRequestMapper.insertOrderRequest(orderRequest);
    }

    /**
     * 修改请购单
     * 
     * @param orderRequest 请购单
     * @return 结果
     */
    @Override
    public int updateOrderRequest(OrderRequest orderRequest)
    {
        orderRequest.setUpdateTime(DateUtils.getNowDate());
        return orderRequestMapper.updateOrderRequest(orderRequest);
    }

    /**
     * 批量删除请购单
     * 
     * @param ids 需要删除的请购单主键
     * @return 结果
     */
    @Override
    public int deleteOrderRequestByIds(String ids)
    {
        String[] idArray = Convert.toStrArray(ids);
        int deleteCount = 0;

        for (String idStr : idArray) {
            Long id = null;
            try {
                id = Long.parseLong(idStr);
            } catch (NumberFormatException e) {
                // id格式不正确，跳过
                continue;
            }

            // 查询请购单，不存在跳过
            OrderRequest orderRequest = this.getById(id);
            if (orderRequest == null) {
                continue;
            }

            // 查询主订单，不存在跳过
            LambdaQueryWrapper<Orders> orderWrapper = new LambdaQueryWrapper<>();
            orderWrapper.eq(Orders::getOrderId, orderRequest.getOrderId());
            Orders order = ordersService.getOne(orderWrapper);
            if (order == null) {
                continue;
            }

            // 订单状态不是“请购”，跳过
            if (order.getStatus() != OrderStatus.REQUESTED) {
                continue;
            }

            // 符合条件，删除请购单
            boolean removed = this.removeById(id);
            if (removed) {
                deleteCount++;
                // 更新主订单状态为草稿
                order.setStatus(OrderStatus.DRAFT);
                ordersService.updateById(order);
            }
        }

        return deleteCount;
    }


    /**
     * 删除请购单信息
     * 
     * @param id 请购单主键
     * @return 结果
     */
    @Override
    public int deleteOrderRequestById(Long id)
    {
        return orderRequestMapper.deleteOrderRequestById(id);
    }

    @Override
    public void createOrderRequest(Orders order) {
        if (order.getOrderId() == null) {
            throw new ServiceException("订单编号不能为空");
        }
        OrderRequest request = new OrderRequest();
        request.setOrderId(order.getOrderId());
        request.setProductName(order.getProductName());
        request.setCreateTime(DateUtils.getNowDate());
        request.setUpdateTime(DateUtils.getNowDate());
        this.save(request);
    }

    @Override
    public List<OrderRequest> selectOrderRequestByIds(List<String> ids) {
        return orderRequestMapper.selectOrderRequestByIds(ids);
    }
}
